package com.dys.hejiayuncommunity.web.controller.system;

import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.utils.ChainedMap;
import com.dys.hejiayuncommunity.common.utils.ServletUtils;
import com.dys.hejiayuncommunity.framework.service.SysPermissionService;
import com.dys.hejiayuncommunity.system.domain.LoginUser;
import com.dys.hejiayuncommunity.system.domain.SysMenu;
import com.dys.hejiayuncommunity.system.domain.SysUser;
import com.dys.hejiayuncommunity.system.domain.vo.LoginBody;
import com.dys.hejiayuncommunity.system.domain.vo.RouterVo;
import com.dys.hejiayuncommunity.system.service.SysLoginService;
import com.dys.hejiayuncommunity.system.service.SysMenuService;
import com.dys.hejiayuncommunity.system.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @className: com.dys.hejiayuncommunity.web.controller.system.SysLoginController
 * @description: 安全验证控制器
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/12/19 22:39
 */
@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping(value = "/login")
    public ChainedMap login(@RequestBody LoginBody loginBody) {
        String token = sysLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        return ChainedMap.create().set("token", token);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping(value = {"/getInfo"})
    public ChainedMap getInfo() {
        // 1.获取用户信息
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getSysUser();
        // 2.角色集合
        Set<String> rolePermission = sysPermissionService.getRolePermission(user);
        // 3.菜单(权限)集合
        Set<String> menuPermission = sysPermissionService.getMenuPermission(user);

        ChainedMap chainedMap = ChainedMap.create().set("code", 200).set("msg", "操作成功");
        chainedMap.put("user", user);
        chainedMap.put("roles", rolePermission);
        chainedMap.put("permissions", menuPermission);
        return chainedMap;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping(value = {"/getRouters"})
    public BaseResponse<List<RouterVo>> getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getSysUser();
        // 获取菜单列表
        List<SysMenu> menuList = sysMenuService.selectMenuTreeByUserId(user.getUserId());
        // 转换为前端需要的路由列表
        List<RouterVo> routerVoList = sysMenuService.buildMenus(menuList);
        return BaseResponse.success(routerVoList);
    }
}
